# -*- coding: utf-8 -*-

import requests
import json
from pyquery import PyQuery as pq
from decimal import *
from datetime import datetime
from mythread import MyThread
import time


bittrex_buyin_coin_type = ["XCP", "DOGE", "BTS", "ZEC", "ETC", "XEM", "FTC", "XMR", "NXT", "DASH", "PPC", "REP", "ETH"]
bittrex_exchange_coin_type = ["BTC"]

poloniex_buyin_coin_type = ["XCP", "DOGE", "BTS", "ZEC", "ETC", "XEM", "XMR", "NXT", "DASH", "PPC", "REP", "ETH"]

def bittrex_exchange_queue(buyin,sell):
    btce_ltc2btc_url = "https://bittrex.com/api/v1.1/public/getorderbook?market=%s-%s&type=both&depth=100" % (sell , buyin)
    try:
        btce_body = requests.get(btce_ltc2btc_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
    except:
        print "except bittrex_btc2xcp_queue"
        return None
    return btce_obj

def bter_sell_depthq(sell):
    btce_btc2usd_url = "http://data.bter.com/api/1/depth/%s_cny" % sell
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
        btce_obj["asks"].reverse()
    except:
        print "except bter_btc2cny_depthq"
        return None
    return btce_obj

def bter_buyin2cny_depthq(buyin):
    btce_btc2usd_url = "http://data.bter.com/api/1/depth/%s_cny" % buyin
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
        btce_obj["asks"].reverse()
    except:
        print "except bter_btc2cny_depthq"
        return None
    return btce_obj


def poloniex_exchange_queue(buyin,sell):
    btce_ltc2btc_url = "https://poloniex.com/public?command=returnOrderBook&currencyPair=%s_%s&depth=100" % (sell, buyin)
    try:
        btce_body = requests.get(btce_ltc2btc_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
    except:
        print "except bittrex_btc2xcp_queue"
        return None
    return btce_obj

###################

def btce_ltc2btc_queue():
    btce_ltc2btc_url = "https://btc-e.com/api/3/depth/ltc_btc"
    try:
        btce_body = requests.get(btce_ltc2btc_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
    except:
        print "except btce_ltc2btc"
        return None
    return btce_obj




def btce_btc2usd_depthq():
    btce_btc2usd_url = "https://btc-e.com/api/3/depth/btc_usd"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
    except:
        print "except btce_btc2usd_depthq"
        return None
    return btce_obj


def btce_eth2usd_depthq():
    btce_btc2usd_url = "https://btc-e.com/api/3/depth/eth_usd"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
    except:
        print "except btce_eth2usd_depthq"
        return None
    return btce_obj

def btce_eth2ltc_depthq():
    btce_btc2usd_url = "https://btc-e.com/api/3/depth/eth_ltc"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
    except:
        print "except btce_eth2usd_depthq"
        return None
    return btce_obj


def stamp_btc2usd_depthq():
    btce_btc2usd_url = "https://www.bitstamp.net/api/v2/order_book/btcusd/"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
    except:
        print "except stamp_btc2usd_depthq"
        return None
    return btce_obj

def stamp_xrp2usd_depthq():
    btce_btc2usd_url = "https://www.bitstamp.net/api/v2/order_book/xrpusd/"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
    except:
        print "except stamp_btc2usd_depthq"
        return None
    return btce_obj


def okcoin_btc2cny_depthq():
    btce_btc2usd_url = "https://www.okcoin.cn/api/v1/depth.do?symbol=btc_cny"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
        btce_obj["asks"].reverse()
    except:
        print "except stamp_btc2usd_depthq"
        return None
    return btce_obj

def okcoin_ltc2cny_depthq():
    btce_btc2usd_url = "https://www.okcoin.cn/api/v1/depth.do?symbol=ltc_cny"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
        btce_obj["asks"].reverse()
    except:
        print "except stamp_btc2usd_depthq"
        return None
    return btce_obj

def biter_eth2cny_depthq():
    btce_btc2usd_url = "http://data.bter.com/api/1/depth/eth_cny"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
        btce_obj["asks"].reverse()
    except:
        print "except biter_eth2cny_depthq"
        return None
    return btce_obj



def biter_ltc2cny_depthq():
    btce_btc2usd_url = "http://data.bter.com/api/1/depth/ltc_cny"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
        btce_obj["asks"].reverse()
    except:
        print "except biter_eth2cny_depthq"
        return None
    return btce_obj



def coinone_btc2kwr_depthq():
    btce_btc2usd_url = "https://api.coinone.co.kr/orderbook/?currency=btc&format=json"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
    except:
        print "except coinone_btc2usd_bid_depthq"
        return None
    return btce_obj


def coinone_eth2kwr_depthq():

    btce_btc2usd_url = "https://api.coinone.co.kr/orderbook/?currency=eth&format=json"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
    except:
        print "except coinone_eth2usd_bid_depthq"
        return None
    return btce_obj

def coinone_xrp2usd_bid_depthq():

    btce_btc2usd_url = "https://api.coinone.co.kr/orderbook/?currency=xrp&format=json"
    try:
        btce_body = requests.get(btce_btc2usd_url, timeout=8)
        btce_obj = json.loads(btce_body.content)
    except:
        print "except coinone_eth2usd_bid_depthq"
        return None
    return btce_obj


def lh_btc2cny_price():
    lh_btc2cny_url = "https://api.lhang.com/v1/ticker.do?symbol=btc_cny"
    try:
        lh_btc2cny_body = requests.get(lh_btc2cny_url, timeout=8)
        lh_btc2cny_obj = json.loads(lh_btc2cny_body.content)
    except:
        print "except lh_btc2cny_price"
        return Decimal(-1)
    return Decimal(lh_btc2cny_obj["ticker"]["latest"])

def bhumb_btc2usd_price():
    bhumb_btc2usd_url = "https://api.bithumb.com/public/ticker/btc"
    try:
        bhumb_btc2usd_body = requests.get(bhumb_btc2usd_url, timeout=8)
        bhumb_btc2usd_obj = json.loads(bhumb_btc2usd_body.content)
        if bhumb_btc2usd_obj["status"] != 0:
            return Decimal(-1)
    except:
        print "except bhumb_btc2usd_price"
        return Decimal(-1)
    return Decimal(bhumb_btc2usd_obj["data"]["sell_price"])

def korbit_btc2kwr_price():
    korbit_btc2usd_url = "https://api.korbit.co.kr/v1/ticker"
    try:
        korbit_btc2usd_body = requests.get(korbit_btc2usd_url, timeout=8)
        korbit_btc2usd_obj = json.loads(korbit_btc2usd_body.content)
    except:
        print "except korbit_btc2kwr_price"
        return Decimal(-1)
    return Decimal(korbit_btc2usd_obj["last"])

def bfinex_ltc2btc():
    bfinex_ltc2btc_url = "https://api.bitfinex.com/v1/pubticker/ltcbtc"
    try:
        bfinex_body = requests.get(bfinex_ltc2btc_url, timeout=8)
        bfinex_obj = json.loads(bfinex_body.content)
    except:
        print "except bfinex_ltc2btc"
        return Decimal(-1)
    return Decimal(bfinex_obj["last_price"])

def coinone_btc2kwr_price():
    coinone_btc2usd_url = "https://api.coinone.co.kr/ticker/?currency=btc&format=json"
    try:
        coinone_btc2usd_body = requests.get(coinone_btc2usd_url, timeout=8)
        coinone_btc2usd_obj = json.loads(coinone_btc2usd_body.content)
    except:
        print "except coinone_btc2kwr_price"
        return Decimal(-1)
    return Decimal(coinone_btc2usd_obj["last"])

def coinone_eth2kwr_price():
    coinone_btc2usd_url = "https://api.coinone.co.kr/ticker/?currency=eth&format=json"
    try:
        coinone_btc2usd_body = requests.get(coinone_btc2usd_url, timeout=8)
        coinone_btc2usd_obj = json.loads(coinone_btc2usd_body.content)
    except:
        print "except coinone_eth2kwr_price"
        return Decimal(-1)
    return Decimal(coinone_btc2usd_obj["last"])

def stamp_btc2usd_price():
    stamp_btc2usd_url = "https://www.bitstamp.net/api/v2/ticker/btcusd"
    try:
        stamp_btc2usd_body = requests.get(stamp_btc2usd_url, timeout=8)
        stamp_btc2usd_obj = json.loads(stamp_btc2usd_body.content)
    except:
        print "except stamp_btc2usd_price"
        return Decimal(-1)
    return Decimal(stamp_btc2usd_obj["last"])

def bitflyer_eth2btc():
    bitflyer_btc2usd_url = "https://api.bitflyer.jp/v1/ticker?product_code=ETH_BTC"
    try:
        bitflyer_btc2usd_body = requests.get(bitflyer_btc2usd_url, timeout=8)
        bitflyer_btc2usd_obj = json.loads(bitflyer_btc2usd_body.content)
    except:
        print "except stamp_btc2usd_price"
        return None
    return bitflyer_btc2usd_obj

usd_huilv = Decimal(-1);

def get_usd2cny():
    return usd_huilv

def set_usd2cny(usd):
    global usd_huilv
    usd_huilv = Decimal(usd)

def get_krw2cny():
    return Decimal(1)/Decimal(170)

def get_info():
    mt = MyThread()
    g_func_list = []

    for ir in bittrex_exchange_coin_type:
        for it in bittrex_buyin_coin_type:
            g_func_list.append({"name":"bittrex_%s_%s" % (ir,it),"func": bittrex_exchange_queue, "args": (it,ir)})

    for ir in bittrex_exchange_coin_type:
        for it in poloniex_buyin_coin_type:
            g_func_list.append({"name": "poloniex_%s_%s" % (ir, it), "func": poloniex_exchange_queue, "args": (it, ir)})

    for it in bittrex_buyin_coin_type:
        g_func_list.append({"name": "bter_%s" % it, "func": bter_buyin2cny_depthq, "args": (it,)})

    for it in bittrex_exchange_coin_type:
        g_func_list.append({"name": "bter_%s" % it, "func": bter_sell_depthq, "args": (it,)})

    mt.set_thread_func_list(g_func_list)
    mt.start()
    return mt.ret_func





if __name__ == '__main__':
    pass
